package com.compomics.prioritizer.general;

import javax.swing.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Niklaas Colaert
 * Date: 5-dec-2008
 * Time: 13:51:55
 * To change this template use File | Settings | File Templates.
 */
public class ArrayListComboBoxModel
        extends AbstractListModel
        implements MutableComboBoxModel {

    private Object selectedItem;

    private List iList;

    public ArrayListComboBoxModel(List aList) {
        this.iList = aList;
    }

    // ListModel
    public int getSize() {
       return iList.size();
    }

    public Object getElementAt(int i) {
        return iList.get(i);
    }

    // ComboBoxModel

    public Object getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(Object newValue) {
        selectedItem = newValue;
    }


    public void addElement(Object element) {
        iList.add(element);
        int length = getSize();
        fireIntervalAdded(this, length - 1, length - 1);
    }

    public void insertElementAt(Object element, int index) {
        iList.add(index,element);   
        fireIntervalAdded(this, index, index);
    }

    public void removeElement(Object element) {
        // Find out position
        int index = iList.indexOf(element);

        if (index != -1) {

            iList.remove(element);

            fireIntervalRemoved(this, index, index);
        }
    }

    public void removeElementAt(int index) {
        if (getSize() >= index) {

            // Remove an element at the specified position

            // Removed from index, notify ListDataListener objects
            fireIntervalRemoved(this, index, index);
        }
    }
}
